home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc International / Development / TSMTEsample⁄1.1 / Source / SampleCollections.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-14  |  7.7 KB  |  416 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Collections.h
  3.  
  4.     Contains:    Sample collection functions & classes
  5.  
  6.     Written by:    Steve Smith
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Description:
  11.                 CList: Generic unordered list
  12.                 COrderedList: Generic ordered list
  13.                 CFrameList: Unordered list of frames -
  14.                             frames automatically refcounted when 
  15.                             added/removed from list.
  16.                 CQueue: Generic queue collection
  17.                 CStack: Generic stack collection
  18. */
  19.  
  20.  
  21. #ifndef _SAMPLECOLLECTIONS_
  22. #define _SAMPLECOLLECTIONS_
  23.  
  24. // --- OpenDoc Includes ---
  25.  
  26. #ifndef _ODTYPES_
  27. #include <ODTypes.h>
  28. #endif
  29.  
  30. #ifndef SOM_ODFrame_xh
  31. #include <Frame.xh>
  32. #endif
  33.  
  34. // --- OpenDoc Utilities ---
  35.  
  36. #ifndef _ODNEW_
  37. #include <ODNew.h>
  38. #endif
  39.  
  40. #ifndef _LINKLIST_
  41. #include "LinkList.h"
  42. #endif
  43.  
  44. // --- Macintosh Toolbox ---
  45.  
  46. #ifndef _LIMITS_
  47. #include <Limits.h>
  48. #endif
  49.  
  50. //------------------------------------------------------------------------------
  51. // Classes Defined by this Interface
  52. //------------------------------------------------------------------------------
  53.  
  54. class CList;
  55. class CListIterator;
  56. class COrderedList;
  57. class COrdListIterator;
  58. class CFrameList;
  59. class CFrameListIterator;
  60. class CQueue;
  61. class CStack;
  62.  
  63. //------------------------------------------------------------------------------
  64. // Forwards
  65. //------------------------------------------------------------------------------
  66.  
  67. enum {
  68.     kItemNotFound = -1,
  69.     kListIsEmpty = 0
  70. };
  71.  
  72. //------------------------------------------------------------------------------
  73. // Collection Class Definitions
  74. //------------------------------------------------------------------------------
  75.  
  76. class CGenericLink: public Link {
  77.     public:
  78.     CGenericLink();
  79.     CGenericLink(ODPtr value);
  80.     virtual ~CGenericLink();
  81.     
  82.     ODPtr    GetValue();
  83.     void    SetValue(ODPtr value);
  84.     
  85.     protected:
  86.     ODPtr    fValue;
  87. };
  88.  
  89. class CFrameLink : public CGenericLink {
  90.     
  91.     public:
  92.     CFrameLink();
  93.     CFrameLink(ODFrame* frame);
  94.     virtual ~CFrameLink();
  95.         
  96.     ODFrame*    GetFrame();
  97.     void        SetFrame(ODFrame* frame);
  98. };
  99.  
  100.  
  101. class CList {
  102.     public:
  103.     CList();
  104.     virtual ~CList();
  105.  
  106.       ODBoolean    IsEmpty() const;
  107.     ODULong        Count() const;
  108.       ODBoolean    Contains(const ODPtr value);
  109.       void        DeleteAllLinks();
  110.       void        RemoveAllLinks();
  111.       void        Delete(ODPtr value);
  112.       void        Remove(ODPtr value);
  113.       void        Add(ODPtr value);
  114.             
  115.       private:
  116.       LinkedList        fList;
  117.       
  118.       friend class CListIterator;
  119. };
  120.  
  121. class CListIterator {
  122.     public:
  123.     CListIterator() {}
  124.     CListIterator(CList* list);
  125.     virtual ~CListIterator();
  126.         
  127.     ODPtr        First();
  128.     ODPtr        Next();
  129.     ODPtr        Previous();
  130.     ODPtr        Last();
  131.     ODPtr        Current();
  132.     ODBoolean     IsNotComplete();
  133.     void        RemoveCurrent();
  134.     void        DeleteCurrent();
  135.  
  136.     protected:
  137.     LinkedListIterator*    fIter;
  138. };
  139.  
  140.  
  141. class COrderedList {
  142.     public:
  143.     COrderedList();
  144.     virtual ~COrderedList();
  145.  
  146.       ODBoolean    IsEmpty() const;
  147.     ODULong        Count() const;
  148.       ODBoolean    Contains(const ODPtr value);
  149.       ODUShort    Position(const ODPtr value);
  150.       void        DeleteAllLinks();
  151.       void        RemoveAllLinks();
  152.       void        Delete(ODPtr valeu);
  153.       void        Remove(ODPtr value);
  154.       ODPtr        RemoveFirst();
  155.       ODPtr        RemoveLast();
  156.       void        AddBefore(const ODPtr existing, ODPtr value);
  157.       void        AddAfter(const ODPtr existing, ODPtr value);
  158.       void        AddFirst(ODPtr value);
  159.       void        AddLast(ODPtr value);
  160.       ODPtr        After(const ODPtr value) const;
  161.       ODPtr        Before(const ODPtr value) const;
  162.       ODPtr        First() const;
  163.       ODPtr        Last() const;
  164.             
  165.       private:
  166.       LinkedList        fList;
  167.       
  168.       friend class COrdListIterator;
  169. };
  170.  
  171. class COrdListIterator : public CListIterator {
  172.     public:
  173.     COrdListIterator(COrderedList* list);
  174.     virtual ~COrdListIterator();
  175. };
  176.  
  177.  
  178. class CFrameList {
  179.     public:
  180.     CFrameList();
  181.     virtual ~CFrameList();
  182.  
  183.       ODBoolean    IsEmpty() const;
  184.     ODULong        Count() const;
  185.       ODBoolean    Contains(const ODFrame* frame);
  186.       void        Remove(ODFrame* frame);
  187.       void        Add(ODFrame* frame);
  188.     ODFrame*    GetFrame();
  189.  
  190.       private:
  191.       LinkedList        fList;
  192.     
  193.       friend class CFrameListIterator;
  194. };
  195.  
  196. class CFrameListIterator {
  197.     public:
  198.     CFrameListIterator(CFrameList* list);
  199.     ~CFrameListIterator();
  200.         
  201.     ODFrame*    First();
  202.     ODFrame*    Next();
  203.     ODFrame*    Previous();
  204.     ODFrame*    Last();
  205.     ODFrame*    Current();
  206.     ODBoolean     IsNotComplete();
  207.     void        ReleaseCurrent();
  208.  
  209.     private:
  210.     LinkedListIterator*    fIter;
  211. };
  212.  
  213. class CStack {
  214.     public:
  215.     CStack();
  216.     ~CStack();
  217.     
  218.     ODBoolean    IsEmpty();
  219.     void        EmptyStack(ODBoolean deleteEntries = kODFalse);
  220.     ODUShort    SetSize(ODUShort maxDepth);
  221.     ODBoolean    PushEntry(ODPtr entry);
  222.     ODPtr        PopEntry();
  223.     
  224.     private:
  225.     ODUShort        fMaxDepth;
  226.     LinkedList        fStack;
  227. };
  228.  
  229. class CQueue {
  230.     public:
  231.     CQueue();
  232.     ~CQueue();
  233.  
  234.     ODBoolean    IsEmpty();
  235.     void        EmptyQueue(ODBoolean deleteEntries = kODFalse);
  236.     ODUShort    SetSize(ODUShort maxEntries);
  237.     ODBoolean    AddEntry(ODPtr entry);
  238.     ODPtr        GetEntry();
  239.     
  240.     private:
  241.     ODUShort        fMaxEntries;
  242.     LinkedList        fQueue;
  243. };
  244.  
  245. //-------------------------------------------------------------------
  246. // Inline methods
  247. //-------------------------------------------------------------------
  248.  
  249. //====================================================================
  250. // CGenericLink
  251. //====================================================================
  252.  
  253. inline CGenericLink::CGenericLink()
  254.     :Link()
  255. {
  256.     fValue = kODNULL;
  257. }
  258.  
  259. inline CGenericLink::CGenericLink(ODPtr value)
  260.     :Link()
  261. {
  262.     fValue = value;
  263. }
  264.  
  265. inline CGenericLink::~CGenericLink()
  266. {
  267. }
  268.  
  269. inline ODPtr CGenericLink::GetValue()
  270. {
  271.     return fValue;
  272. }
  273.  
  274. inline void CGenericLink::SetValue(ODPtr value)
  275. {
  276.     fValue = value;
  277. }
  278.  
  279. //====================================================================
  280. // CFrameLink
  281. //====================================================================
  282.  
  283. inline CFrameLink::CFrameLink()
  284.     :CGenericLink()
  285. {
  286. }
  287.  
  288. //====================================================================
  289. // CList
  290. //====================================================================
  291.  
  292. inline CList::CList()
  293. {
  294. }
  295.  
  296. inline CList::~CList()
  297. {
  298.     fList.DeleteAllLinks();
  299. }
  300.  
  301. inline ODBoolean CList::IsEmpty() const
  302. {
  303.     return fList.IsEmpty();
  304. }
  305.  
  306. inline ODULong CList::Count() const
  307. {
  308.     return fList.Count();
  309. }
  310.  
  311. inline void CList::RemoveAllLinks()
  312. {
  313.     fList.RemoveAll();
  314. }
  315.  
  316. //====================================================================
  317. // COrderedList
  318. //====================================================================
  319.  
  320. inline COrderedList::COrderedList()
  321. {
  322. }
  323.  
  324. inline COrderedList::~COrderedList()
  325. {
  326.     fList.DeleteAllLinks();
  327. }
  328.  
  329. inline ODBoolean COrderedList::IsEmpty() const
  330. {
  331.     return fList.IsEmpty();
  332. }
  333.  
  334. inline ODULong COrderedList::Count() const
  335. {
  336.     return fList.Count();
  337. }
  338.  
  339. inline void COrderedList::RemoveAllLinks()
  340. {
  341.     fList.RemoveAll();
  342. }
  343.  
  344. //====================================================================
  345. // COrdListIterator
  346. //====================================================================
  347.  
  348. inline COrdListIterator::COrdListIterator(COrderedList* list)
  349. {
  350.     fIter = new LinkedListIterator(&list->fList);
  351. }
  352.  
  353. inline COrdListIterator::~COrdListIterator()
  354. {
  355. }
  356.     
  357. //====================================================================
  358. // CFrameList
  359. //====================================================================
  360.  
  361. inline CFrameList::CFrameList()
  362. {
  363. }
  364.  
  365. inline ODBoolean CFrameList::IsEmpty() const
  366. {
  367.     return fList.IsEmpty();
  368. }
  369.  
  370. inline ODULong CFrameList::Count() const
  371. {
  372.     return fList.Count();
  373. }
  374.  
  375. //====================================================================
  376. // CStack
  377. //====================================================================
  378.  
  379. inline CStack::CStack()
  380. {
  381.     fMaxDepth = USHRT_MAX;
  382. }
  383.  
  384. inline CStack::~CStack()
  385. {
  386.     fStack.DeleteAllLinks();
  387. }
  388.  
  389. inline ODBoolean CStack::IsEmpty()
  390. {
  391.     return fStack.IsEmpty();
  392. }
  393.  
  394. //====================================================================
  395. // CQueue
  396. //====================================================================
  397.  
  398. inline CQueue::CQueue()
  399. {
  400.     fMaxEntries = USHRT_MAX;
  401. }
  402.  
  403. inline CQueue::~CQueue()
  404. {
  405.     fQueue.DeleteAllLinks();
  406. }
  407.  
  408. inline ODBoolean CQueue::IsEmpty()
  409. {
  410.     return fQueue.IsEmpty();
  411. }
  412.  
  413.  
  414. #endif //_SAMPLECOLLECTIONS_
  415.  
  416.